<?php

/**
 * @copyright 2011
 * @version $Id$
 * @access public
 * @author oriotiori

 */

class Admin_Model_Company extends Zend_Db_Table_Abstract{

	protected $_name = 'company';

	/**
	 * @return array $data
	 */
	public function getCompanyInformation($id=0)
	{
		$TO = new Admin_Model_TO_Company();

		$data=array();
		try{
			$sql=$this->getAdapter()->select()->from(TABLE_PREFIX.$this->_name)->where('Id=?', $id);

			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setCompany_Name($dt['Company_Name']);
					$TO->setInformation_Email($dt['Information_Email']);
					$TO->setInformation_Name($dt['Information_Name']);
					$TO->setContact_Email($dt['Contact_Email']);
					$TO->setOwner_Email($dt['Owner_Email']);
					$TO->setOwner_Name($dt['Owner_Name']);
					$TO->setUrl_Site($dt['Url_Site']);
				}
				$data = $TO->getAllVars();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * @return array $data
	 */
	public function getCompanys()
	{
		$TO = new Admin_Model_TO_Company();

		$data=array();
		try{
			$sql=$this->getAdapter()->select()->from(array('c' => TABLE_PREFIX.$this->_name), 'c.*')
			->joinLeft(array('s' => TABLE_PREFIX.'admin_seo'), 's.Company_Id=c.Id', array('SeoId' => 's.Id') )
			;

			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setCompany_Name($dt['Company_Name']);
					$TO->setInformation_Email($dt['Information_Email']);
					$TO->setInformation_Name($dt['Information_Name']);
					$TO->setContact_Email($dt['Contact_Email']);
					$TO->setOwner_Email($dt['Owner_Email']);
					$TO->setOwner_Name($dt['Owner_Name']);
					$TO->setUrl_Site($dt['Url_Site']);
					$TO->setSeoId($dt['SeoId']);
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}




	/**
	 * @return array $data
	 */
	public function updateCompanyInformationById($to=array())
	{
		$TO = new Admin_Model_TO_Company();

		try{
			$data= array(
				"Company_Name" 		=> $to['CompanyName'],
				"Information_Email" => $to['InformationEmail'],
				"Contact_Email" 	=> $to['ContactEmail'],
				"Owner_Email" 		=> $to['OwnerEmail'],
				"Owner_Name" 		=> $to['OwnerName'],
				"Url_Site" 			=> $to['UrlSite'],
				"Information_Name" 	=> $to['InformationName'],
			);
			
			$where[]="Id=".$to['Id'];
			$res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name ,$data, $where);
			return true;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}




}
